package com.wngz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wngz.dto.EmployeeDto;
import com.wngz.entity.Employee;
import com.wngz.mapper.EmployeeMapper;
import com.wngz.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Objects;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    EmployeeMapper employeeMapper;
    @Override
    public IPage<Employee> queryEmployees(EmployeeDto employeeDto) {
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();

        // 拼接查询条件
        queryWrapper.like(StringUtils.hasText(employeeDto.getName()), Employee::getName, employeeDto.getName())
                .eq(StringUtils.hasText(employeeDto.getSex()), Employee::getSex, employeeDto.getSex())
                .eq(StringUtils.hasText(employeeDto.getDepart()), Employee::getDepart, employeeDto.getDepart())
                .eq(Objects.nonNull(employeeDto.getSalary()), Employee::getSalary, employeeDto.getSalary());

        Page<Employee> objectPage = new Page<>(employeeDto.getPageNumber(),employeeDto.getPageSize());
        employeeMapper.selectPage(objectPage, queryWrapper);

        return objectPage;
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.updateById(employee);
    }
}
